﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Table_Dal;
using DAL;
namespace BLL
{
    public class NguoiradeBll
    {
        public NguoiradeTbl ThongTin(string user)
        {
            NguoiradeDAL nrd = new NguoiradeDAL();
            return nrd.ThongTin(user);
            
        }
        public NguoiradeTbl THongTin_MadangNhap(string id)
        {
            NguoiradeDAL nrd = new NguoiradeDAL();

            return nrd.ThongTin(nrd.Madangnhap(id));

        }
        public string Manguoirade(string id)
        {
            NguoiradeDAL nrd = new NguoiradeDAL();

            return nrd.Manguoirade(id);
        }
        public bool ThemNguoiRade(NguoiradeTbl nrd)
        {
            QuantriDal qtri = new QuantriDal();
            BusinessLogicLayer bll = new BusinessLogicLayer();
            string x = bll.RandomMa();
            while(qtri.KiemTra_TrungKhoa(x)){
                x = bll.RandomMa();
            }
            nrd.MANGUOIRADE = x;
            QuanTriTbl qt = new QuanTriTbl();
            qt.MADANGNHAP = nrd.MADANGNHAP;
            qt.MATKHAU = nrd.MATKHAU;
            qt.PHANQUYEN = 1;
            
            qtri.ThemQuanTri(qt);
            NguoiradeDAL nrddal = new NguoiradeDAL();
            return nrddal.ThemNguoirade(nrd);
        }
        public bool SuaNguoirade(NguoiradeTbl nrd)
        {
            NguoiradeDAL nrddal = new NguoiradeDAL();
            return nrddal.Sua(nrd);
        }
        public string PictureLink(int temp,string manguoirade)
        {
            if (temp == 0)
            {
                return @"HINHANH\NGUOIRADE\NoImage.jpg";
            }
            else
            {
            NguoiradeDAL chdal = new NguoiradeDAL();
            return chdal.PictureLink(manguoirade);
            }
            
        }
        public List<NguoiradeTbl> DanhSachNguoiRade_PhanQuyen(int quyen)
        {
            NguoiradeDAL ngdal = new NguoiradeDAL();
            return ngdal.DSNguoiRade_PhanQuyen(quyen);
        }
        public bool Sua(NguoiradeTbl nrd)
        {
            NguoiradeDAL ngdal = new NguoiradeDAL();
            return ngdal.Sua(nrd);
        }
        public bool Xoa(string manguoirade)
        {  
            NguoiradeDAL ngdal = new NguoiradeDAL();
            BusinessLogicLayer bll = new BusinessLogicLayer();
            if(ngdal.PictureLink( manguoirade).Trim().Length>0)
                bll.XoaPicture(ngdal.PictureLink(manguoirade).Trim());
            return ngdal.Xoa(manguoirade);
        }
    }
}
